
#include <string>
#include <iostream>
#include "Interpreter.h"
#include <fstream>

int main(int argc, char** argv)
{
	bool printUsageAndExit = false;
	if(argc != 3)
	{
		printUsageAndExit = true;
	}
	else
	{
		if(argv[1][0] != '-')
		{
			printUsageAndExit = true;
		}
		else
		{
			Interpreter* ip = Interpreter::GetInstance();
			if(argv[1][1] == 'f')
			{
				std::ifstream file(argv[2]);
				std::string query;
				int accumulatingDiskIOs = 0;
				while(1)
				{
					std::getline(file, query);
					if(file.good() == false)
						break;

					if(query[0] == '#') // comments the query line
						continue;

					std::cout << "\n\nInterpreting query.....\n" << query << std::endl;

					if(ip->Interpret(query) != 0)
						std::cout << "Query Execution Error !@#$%^&*()" << std::endl;

					std::cout << "Disk I/Os for this query: " << 
						ip->GetDiskIOs() - accumulatingDiskIOs << std::endl;

					accumulatingDiskIOs = ip->GetDiskIOs();
				}
			}
			else if(argv[1][1] == 'q')
			{
				std::string query(argv[2]);
				std::cout << "Interpreting query.....\n" << query << std::endl;
				ip->Interpret(query);
			}
			else
			{
				printUsageAndExit = true;
			}
			std::cout << "\n\nTotal disk I/Os: " << ip->GetDiskIOs() << std::endl;
			Interpreter::Destroy();
		}
	}

	if(printUsageAndExit)
	{
		std::cout << "Usage:" << std::endl;
		std::cout << argv[0] << " -f fileWithQueries" << std::endl;
		std::cout << argv[0] << " -q \"Query\"" << std::endl;
		return -1;
	}
	
	return 0;
}
